package com.dog.basics.rabbitmq.config;


import com.dog.basics.rabbitmq.callback.RabbitConfirmCallback;
import com.dog.basics.rabbitmq.callback.RabbitReturnCallback;
import lombok.AllArgsConstructor;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: d-basic-composition
 *
 * @description: 生产者 - 生产者配置
 *
 * @author: Guo
 *
 * @create: 2019-08-10 11:50
 **/
@Configuration
@AllArgsConstructor
public class RabbitmqConfiguration {

	private final RabbitTemplate rabbitTemplate;

	@PostConstruct
	public void initRabbitTemplate() {
		// 设置生产者消息确认
		rabbitTemplate.setConfirmCallback(new RabbitConfirmCallback());
		rabbitTemplate.setReturnCallback(new RabbitReturnCallback());
	}


	/**              下面为测试声明                */


	//-----------------------------------------------------------------声明普通队列.开始-----------------------------------------------------------------------------
	/**
	 * 申明队列
	 *
	 * @return
	 */
	//@Bean
	public Queue queue() {
		Map<String, Object> arguments = new HashMap<>(4);
		// 申明死信交换器
		arguments.put("x-dead-letter-exchange", "exchange-dlx");
		return new Queue("queue-rabbit-springboot-advance", true, false, false, arguments);
	}

	/**
	 * 申明交换器
	 *
	 * @return
	 */
	//@Bean
	public Exchange exchange() {
		Map<String, Object> arguments = new HashMap<>(4);
		// 当发往exchange-rabbit-springboot-advance的消息,routingKey和bindingKey没有匹配上时，将会由exchange-unroute交换器进行处理
		arguments.put("alternate-exchange", "exchange-unroute");
		return new DirectExchange("exchange-rabbit-springboot-advance", true, false);
	}

	/**
	 * 申明绑定
	 *
	 * @return
	 */
	//@Bean
	public Binding binding() {
		return BindingBuilder.bind(queue()).to(exchange()).with("product").noargs();
	}
	//-------------------------------------------------------------------声明普通队列.结束---------------------------------------------------------------------------
	//-------------------------------------------------------------------声明无路由队列.开始---------------------------------------------------------------------------

	/**
	 * 没有路由到的消息将进入此队列
	 *
	 * @return
	 */
	//@Bean
	public Queue unRouteQueue() {
		return new Queue("queue-unroute");
	}

	/**
	 * 声明队列
	 * @return
	 */
	//@Bean
	public FanoutExchange unRouteExchange() {
		// 此处的交换器的名字要和 exchange() 方法中 alternate-exchange 参数的值一致
		return new FanoutExchange("exchange-unroute");
	}

	/**
	 * 没有路由到的消息 绑定0
	 * @return
	 */
	//@Bean
	public Binding unRouteBinding() {
		return BindingBuilder.bind(unRouteQueue()).to(unRouteExchange());
	}

	//-------------------------------------------------------------------声明无路由队列.结束---------------------------------------------------------------------------
	//-------------------------------------------------------------------声明死信队列.开始---------------------------------------------------------------------------

	/**
	 * 死信队列
	 *
	 * @return
	 */
	//@Bean
	public Queue dlxQueue() {
		return new Queue("dlx-queue");
	}
	/**
	 * 申明死信交换器
	 *
	 * @return
	 */
	//@Bean
	public FanoutExchange dlxExchange() {
		return new FanoutExchange("exchange-dlx");
	}
	/**
	 * 死信绑定队列
	 * @return
	 */
	//@Bean
	public Binding dlxBinding() {
		return BindingBuilder.bind(dlxQueue()).to(dlxExchange());
	}
	//---------------------------------------------------------------------声明死信队列.结束-------------------------------------------------------------------------



}
